package models

import (
	"time"

	"gorm.io/gorm"
)

// MaRedemptionLog [...]
type MaRedemptionLog struct {
	ID                     string    `gorm:"primaryKey;column:id;type:int;not null"  json:"id" json_old:"ID"`
	MerchantID             string    `gorm:"primaryKey;column:merchant_id;type:varchar(30);not null;comment:商户id" json:"merchant_id"`
	Value                  float64   `gorm:"column:value;type:decimal(10,2);not null"  json:"value" json_old:"Value"`
	Num                    int       `gorm:"column:num;type:int;not null"  json:"num" json_old:"Num"`
	AddTime                time.Time `gorm:"primaryKey;column:add_time;type:datetime;not null"  json:"-" json_old:"-"`
	AddTimeFormated        string    `gorm:"-" json:"add_time" json_old:"AddTime"`
	ExpirationTime         time.Time `gorm:"column:expiration_time;type:datetime;default:null"  json:"-" json_old:"-"`
	ExpirationTimeFormated string    `gorm:"-" json:"expiration_time" json_old:"ExpirationTime"`
	BatchID                string    `gorm:"column:batch_id;type:varchar(30);not null;default:'';comment:批次id" json:"batch_id"`
	CreatedAt              int64     `gorm:"column:created_at;autoCreateTime;type:int;size:64;comment:创建时间" json:"created_at"`
	NewExpirationTime      int64     `gorm:"column:new_expiration_time;type:int;size:64" json:"new_expiration_time"`
}

func (m *MaRedemptionLog) AfterFind(tx *gorm.DB) (err error) {
	if !m.AddTime.IsZero() {
		AddTimeByte, _ := DateTime(m.AddTime).MarshalDateTime()
		m.AddTimeFormated = string(AddTimeByte)
	}
	if !time.Time(m.ExpirationTime).IsZero() {
		ExpirationTimeByte, _ := DateTime(m.ExpirationTime).MarshalDateTime()
		m.ExpirationTimeFormated = string(ExpirationTimeByte)
	}
	return
}

// TableName get sql table name.获取数据库表名
func (m MaRedemptionLog) TableName() string {
	return "ma_redemption_log"
}

// 模式自动注册
func init() {
	Models = append(Models, &MaRedemptionLog{})
}
